package com.ruoyix.ai.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyix.common.helper.LoginHelper;
import com.ruoyix.common.utils.StringUtils;
import com.ruoyix.common.core.page.TableDataInfo;
import com.ruoyix.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyix.ai.domain.bo.AiChatConversationBo;
import com.ruoyix.ai.domain.vo.AiChatConversationVo;
import com.ruoyix.ai.domain.AiChatConversation;
import com.ruoyix.ai.mapper.AiChatConversationMapper;
import com.ruoyix.ai.service.IAiChatConversationService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * AI 聊天会话Service业务层处理
 *
 * @author ruoyi
 * @date 2024-09-21
 */
@RequiredArgsConstructor
@Service
public class AiChatConversationServiceImpl implements IAiChatConversationService {

    private final AiChatConversationMapper baseMapper;

    /**
     * 查询AI 聊天会话
     */
    @Override
    public AiChatConversationVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询AI 聊天会话列表
     */
    @Override
    public TableDataInfo<AiChatConversationVo> queryPageList(AiChatConversationBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<AiChatConversation> lqw = buildQueryWrapper(bo);
        Page<AiChatConversationVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询AI 聊天会话列表
     */
    @Override
    public List<AiChatConversationVo> queryList(AiChatConversationBo bo) {
        LambdaQueryWrapper<AiChatConversation> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<AiChatConversation> buildQueryWrapper(AiChatConversationBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<AiChatConversation> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getUserId() != null, AiChatConversation::getUserId, bo.getUserId());
        lqw.eq(bo.getRoleId() != null, AiChatConversation::getRoleId, bo.getRoleId());
        lqw.eq(StringUtils.isNotBlank(bo.getTitle()), AiChatConversation::getTitle, bo.getTitle());
        lqw.eq(bo.getModelId() != null, AiChatConversation::getModelId, bo.getModelId());
        lqw.eq(StringUtils.isNotBlank(bo.getModel()), AiChatConversation::getModel, bo.getModel());
        lqw.eq(bo.getPinned() != null, AiChatConversation::getPinned, bo.getPinned());
        lqw.eq(bo.getPinnedTime() != null, AiChatConversation::getPinnedTime, bo.getPinnedTime());
        lqw.eq(StringUtils.isNotBlank(bo.getSystemMessage()), AiChatConversation::getSystemMessage, bo.getSystemMessage());
        lqw.eq(bo.getTemperature() != null, AiChatConversation::getTemperature, bo.getTemperature());
        lqw.eq(bo.getMaxTokens() != null, AiChatConversation::getMaxTokens, bo.getMaxTokens());
        lqw.eq(bo.getMaxContexts() != null, AiChatConversation::getMaxContexts, bo.getMaxContexts());
        lqw.eq(bo.getDeleted() != null, AiChatConversation::getDeleted, bo.getDeleted());
        return lqw;
    }

    /**
     * 新增AI 聊天会话
     */
    @Override
    public Boolean insertByBo(AiChatConversationBo bo) {
        AiChatConversation add = BeanUtil.toBean(bo, AiChatConversation.class);
        validEntityBeforeSave(add);
        add.setUserId(LoginHelper.getUserId());
        add.setTemperature(1L);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改AI 聊天会话
     */
    @Override
    public Boolean updateByBo(AiChatConversationBo bo) {
        AiChatConversation update = BeanUtil.toBean(bo, AiChatConversation.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(AiChatConversation entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除AI 聊天会话
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
